package com.hp.gxw.api;

import com.alibaba.fastjson.JSON;
import com.hp.gxw.constant.R;
import com.hp.gxw.u8.EFWebService;
import com.hp.gxw.u8.U8ApiUtils;
import com.hp.gxw.u8.U8WebXMLResponseU8WebXMLResult;
import com.hp.gxw.u8.dto.PodetailsParameter;
import com.hp.gxw.u8.dto.PomainParameter;
import com.hp.gxw.u8.dto.U8ApiResultDto;
import com.hp.gxw.u8.xml.PomainXml;
import com.hp.gxw.utils.DateUtil;
import org.apache.axis.message.MessageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@RequestMapping("/api/pomain")
public class PomainApi {

    @Autowired
    private U8ApiUtils u8ApiUtils;

	@Autowired
	private EFWebService eFWebServiceLocator;

    @Value("${u8client.serviceId}")
    private String serviceId;

    @Value("${u8client.environment}")
    private String environment;

    @RequestMapping("/register")
    @ResponseBody
    public R pomain_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        PomainParameter pomainParameter = new PomainParameter();
        if(map.get("cvencode") == null){
            return R.error("cvencode为必填项");
        }
        pomainParameter.setCvencode(map.get("cvencode").toString());
        if(map.get("dpodate") != null){
            pomainParameter.setDpodate(DateUtil.getDateFormat(map.get("dpodate").toString()));
        }else{
            pomainParameter.setDpodate(new Date());
        }
        pomainParameter.setCbustype("普通采购");
        if(map.get("cdepcode") != null){
            pomainParameter.setCdepcode(map.get("cdepcode").toString());
        }
        if(map.get("cpersoncode") != null){
            pomainParameter.setCpersoncode(map.get("cpersoncode").toString());
        }
        if(map.get("nflat") != null){
            pomainParameter.setNflat(map.get("nflat").toString());
        }else{
            pomainParameter.setNflat("1");
        }
        if(map.get("idiscounttaxtype") != null){
            pomainParameter.setIdiscounttaxtype(map.get("idiscounttaxtype").toString());
        }else{
            pomainParameter.setIdiscounttaxtype("0");
        }
        if(map.get("itaxrate") != null){
            pomainParameter.setItaxrate(map.get("itaxrate").toString());
        }else{
            pomainParameter.setItaxrate("13");
        }
        if(map.get("cexch_name") != null){
            pomainParameter.setCexch_name(map.get("cexch_name").toString());
        }else{
            pomainParameter.setCexch_name("人民币");
        }
        if(map.get("cptcode") == null){
            return R.error("cptcode为必填项");
        }
        pomainParameter.setCptcode(map.get("cptcode").toString());
        if(map.get("cmaker") != null){
            pomainParameter.setCmaker(map.get("cmaker").toString());
        }else{
            pomainParameter.setCmaker("api");
        }
        if(map.get("cmaketime") != null){
            pomainParameter.setCmaketime(DateUtil.getDateTimeFormat(map.get("cmaketime").toString()));
        }else{
            pomainParameter.setCmaketime(new Date());
        }
        if(map.get("cverifier") != null){
            pomainParameter.setCverifier(map.get("cverifier").toString());
        }else{
            pomainParameter.setCverifier("api");
        }
        if(map.get("cauditdate") != null){
            pomainParameter.setCauditdate(DateUtil.getDateFormat(map.get("cauditdate").toString()));
        }else{
            pomainParameter.setCauditdate(new Date());
        }
        if(map.get("caudittime") != null){
            pomainParameter.setCaudittime(DateUtil.getDateTimeFormat(map.get("caudittime").toString()));
        }else{
            pomainParameter.setCaudittime(new Date());
        }

        if(map.get("details") == null){
            return R.error("表体详情数据不能为空");
        }
        List<PodetailsParameter> podetailsParameterList = JSON.parseArray(JSON.toJSON(map.get("details")).toString(), PodetailsParameter.class);
        for(int i=0; i<podetailsParameterList.size(); i++){
            if(podetailsParameterList.get(i).getCinvcode() == null){
                return R.error("表体详情数据第"+i+1+"行cinvcode为空");
            }
            if(podetailsParameterList.get(i).getIquantity() == null || podetailsParameterList.get(i).getIquantity() == 0){
                return R.error("表体详情数据第"+i+1+"行iquantity为空或者为0");
            }
            if(podetailsParameterList.get(i).getDarrivedate() == null){
                podetailsParameterList.get(i).setDarrivedate(new Date());
            }
            if(podetailsParameterList.get(i).getIpertaxrate() == null){
                podetailsParameterList.get(i).setIpertaxrate(pomainParameter.getItaxrate());
            }
        }
        pomainParameter.setPodetailsParameterList(podetailsParameterList);
        pomainParameter.setEfserverid(serviceId);
        if(environment.equals("pro")){
            U8WebXMLResponseU8WebXMLResult u8WebXML = null;
            try {
                u8WebXML = eFWebServiceLocator.getEFWebServiceSoap().u8WebXML(PomainXml.createXML(pomainParameter));
                //解析返回结果
                MessageElement[] data = u8WebXML.get_any();
                System.out.println(data[0].getAttribute("succeed").toString());
                System.out.println(data[0].getAttribute("dsc").toString());
                if("0".equals(data[0].getAttribute("succeed").toString())) {
                    return R.error("接口异常"+data[0].getAttribute("dsc").toString());
                }
            }  catch (Exception e) {
                e.printStackTrace();
                return R.error("接口异常");
            }
        }else if(environment.equals("dev")){
            try {
                U8ApiResultDto resultDto = u8ApiUtils.httpRequest(PomainXml.createXML(pomainParameter).split("\n")[1]);
                if(resultDto == null || "0".equals(resultDto.getCode())) {
                    return R.error(resultDto==null?"接口异常":resultDto.getMsg());
                }
            }catch (Exception e){
                e.printStackTrace();
                return R.error("接口异常");
            }
        }
        return R.ok("写入成功");
    }
}
